import { Config, CONFIG_PROJECT, CONFIG_SEVER } from "../Data/Config";
import { StaticData } from "../Data/StaticData";
import { EventNames } from "../Home/Data/EventNames";
import HttpAPI from "../HttpManager/HttpAPI";
import HttpManager from "../HttpManager/HttpManager";
export class WXLogin {
    // https://h5.9shadow.com/stateGameTest/authorizeUrl?callback=
    private static readonly redirectAPI = CONFIG_SEVER + CONFIG_PROJECT + HttpAPI.GET_AUTHORIZE_URL + "?callback="
    public static WXLogin() {
        return new Promise(async (resolve, reject) => {
            console.log("获取Code");
            let code = this.getUrlParam("code");
            if (!judgeWX()) {
                console.log("非微信环境尝试使用测试Code");
                code = "123456"
            }
            if (code && StaticData.getCode() && StaticData.getCode() == code) {
                // 链接上的code没变
                if (StaticData.getToken()) {
                    console.log("之前有登录成功 再次尝试获取用户信息");
                    // 之前有登录成功 再次尝试获取用户信息
                    await this.getUserData().catch(() => { reject(); })
                    resolve(undefined);
                } else {
                    console.log("之前没有走登录 拿code去登录", code);
                    // 之前没有走登录 拿code去登录
                    await this.login(code).catch(() => { reject(); })
                    await this.getUserData().catch(() => { reject(); })
                    resolve(undefined);
                }
            } else {
                console.log("链接上的 code 变了 ", code);
                if (code) {
                    // code存在
                    console.log("code存在");
                    StaticData.setCode(code);
                    await this.login(code).catch(() => { reject(); })
                    await this.getUserData().catch(() => { reject(); })
                    resolve(undefined);
                } else {
                    console.log("code不存在");
                    // code不存在
                    reject();
                }
            }
        })
    }

    private static urlParam: URLSearchParams
    /** 获取url参数 */
    public static getUrlParam(name: string) {
        if (!this.urlParam) {
            console.log("window.location.href", window.location.href);
            let url = new URL(window.location.href);
            this.urlParam = url.searchParams;

        }
        return this.urlParam.get(name);
    }

    public static login(code: string) {
        return new Promise((resolve, reject) => {
            HttpManager.get(HttpAPI.WX_LOGIN, { code: code }).then(async (res) => {
                if (res.success) {
                    console.log("登录成功");
                    StaticData.setToken(res.result.token); 
                    resolve(undefined);
                } else {
                    Laya.stage.event(EventNames.POPUP_SHOW_COMMON_TIPS, res.message) 
                    reject();
                }
            }), () => { 
                reject();
            }
        })
    }

    public static getUserData() {
        return new Promise((resolve, reject) => {
            HttpManager.get(HttpAPI.GET_USER_INFO).then((res) => {
                if (res.success) {
                    console.log("获取用户信息成功", res);
                    StaticData.nickName = res.result.name;
                    StaticData.headUrl = res.result.avatar;
                    StaticData.phoneNumber = res.result.phone;
                    StaticData.integral = res.result.integral;
                    StaticData.userGameGuide = res.result.guide
                    resolve(res.result);
                } else {
                    console.log("获取用户信息失败", res);
                    Laya.stage.event(EventNames.POPUP_SHOW_COMMON_TIPS, res.message)
                    StaticData.removeToken();
                    reject();
                }
            }).catch(() => {
                console.log("Http Err ", HttpAPI.GET_USER_INFO);
                Laya.stage.event(EventNames.POPUP_SHOW_COMMON_TIPS, "获取用户信息失败, 请检查网络连接")
                StaticData.removeToken();
                reject();
            })
        })
    }

    public static redirect() {
        let url = location.href.split("?")[0];
        let redirectUrl = encodeURIComponent(url);
        let redirectAPI = this.redirectAPI;
        location.href = redirectAPI + redirectUrl;
    }

}